/**
 * @author liubaohui
 * @desc 按指定的单个属性，对对象进行排序
 * @example
 * arr.sort((a, b) => compareFn(a, b, sortKey, sortType))
 * @param a
 * @param b
 * @param {string} sortKey 排序的键名
 * @param {'desc'| 'asc'} sortType 排序方式
 * @returns {number}
 */
export default function compareFn(a, b, sortKey, sortType) {
  let sortValue1 = a[sortKey]
  let sortValue2 = b[sortKey]
  // 排序系数
  let ratio = sortType === 'desc' ? -1 : 1
  if (typeof sortValue1 === 'string') {
    return sortValue1.localeCompare(sortValue2) * ratio
  }
  if (typeof sortValue1 === 'number') {
    return (sortValue1 - sortValue2) * ratio
  }
  return 1
}
